package com.five.community.controller;

import com.five.community.bean.Complaint;
import com.five.community.bean.Repair;
import com.five.community.bean.Village;
import com.five.community.bean.vo.ComplaintVo;
import com.five.community.bean.vo.MailboxVo;
import com.five.community.bean.vo.RepairVo;
import com.five.community.contant.AppContants;
import com.five.community.service.RepairService;
import com.five.community.service.VillageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.Date;
import java.util.List;
@Slf4j
@Controller
@RequestMapping("/repair")
public class RepairController {

    @Autowired
    private RepairService repairService;
    @Autowired
    private VillageService villageService;

//    报修一览
    @RequestMapping("/listUi")
    public String listUi(ModelMap map,Integer pageNum,String name){
        if (pageNum == null) {
            pageNum = 1;
        }

        if (name == null || name == "") {
            try {
                PageInfo<RepairVo> pageInfo = repairService.getPageInfo(pageNum, AppContants.pageSize);
                map.addAttribute("pageInfo",pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            try {
                PageHelper.startPage(pageNum,AppContants.pageSize);
                List<RepairVo> repairVos = repairService.selectByName(name);
                PageInfo<RepairVo> pageInfo = new PageInfo<>(repairVos);
                map.addAttribute("pageInfo", pageInfo);
                Repair repair = new Repair();
                repair.setName(name);
                map.addAttribute("repair", repair);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "repairlist";
    }
    //名字查询界面
//    @RequestMapping("/selectUi")
//    public String selectUi(ModelMap map,String name,Integer pageNum){
//
//        if (name==""){
//            return "redirect:/repair/listUi";
//        }
//        else {
//            try {
//                if (pageNum == null) {
//                    pageNum = 1;
//                }
//                PageHelper.startPage(pageNum,AppContants.pageSize);
//                List<RepairVo> repairVos = repairService.selectByName(name);
//                PageInfo<RepairVo> pageInfo = new PageInfo<>(repairVos);
//                map.addAttribute("pageInfo", pageInfo);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//        return "repairlist";
//    }
//    添加页面
    @RequestMapping("/limit/addUi")
    public String addUi(ModelMap map){
        try {
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages",villages);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "repair_add";
    }
    //添加报修
    @RequestMapping("/add")
    public String add(RepairVo repairVo){
        try {
            long vid = Long.parseLong(repairVo.getVname());
            Repair repair = new Repair(vid,repairVo.getName(),repairVo.getEquipmentName(),repairVo.getDescribes(),new Date(),repairVo.isState());
            repairService.insert(repair);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/repair/addUi";
    }
    //修改编辑页面
    @RequestMapping("/limit/editUi")
    public String editUi(long id, ModelMap map){
        try {
            Repair repair = repairService.selectById(id);
             map.addAttribute("repair",repair);
             List<Village> villages = villageService.selectAll();
             map.addAttribute("villages",villages);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "repair_edit";
    }
    //修改编辑报修
    @RequestMapping("/edit")
    public String edit(RepairVo repairVo){
        try {
            long vid = Long.parseLong(repairVo.getVname());
            Repair repair = new Repair(repairVo.getId(),vid,repairVo.getName(),repairVo.getEquipmentName(),repairVo.getDescribes(),repairVo.getCreateDate(),repairVo.isState());
            repairService.update(repair);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "repair_edit";
    }
    //删除操作
    @RequestMapping("/limit/delete")
    public String delete(long id){
        try {
            repairService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/repair/listUi";
    }
}
